<?php

namespace app\model\order;

use app\base\BaseModel;
use app\libs\Oss;
use think\facade\Db;

/**
 * 模型-PayLogModel
 * Class PayLogModel
 * @package app\model
 */
class PayLogModel extends BaseModel
{

    protected $name = 'pay_log';

    /**
     * 范围查询-order_status
     * @param $query
     * @param $status
     */
    public function scopeStatus($query, $status)
    {
        if ($status != '' && intval($status) >= 0) {
            $query->where('status', '=', $status);
        }
    }

    /**
     * 范围查询-order_number
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('order_sn', '%' . trim($keyword) . '%', 'OR');
            $query->whereLike('pay_sn', '%' . trim($keyword) . '%', 'OR');
        }
    }

    /**
     * 范围查询-pay_time
     * @param $query
     * @param $time
     */
    public function scopePayTime($query, $time = [])
    {
        if (!empty($time)) {
            $query->where('pay_time', 'between', $time);
        }
    }

    /**
     * 获取分页列表
     * @param $status
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @param array $time
     * @param string $pt
     * @return array
     */
    public function getPageData($status, $keyword, $current_page, $list_rows, $field = '*', $time = [])
    {
        $dataList = $this
            ->field($field)
            ->status($status)
            ->keyword($keyword)
            ->payTime($time)
            ->order('id', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        foreach ($items as &$value) {
            $value['pay_time_str'] = "无";
            if (!empty($value['pay_time'])) {
                $value['pay_time_str'] = date("Y-m-d H:i:s", $value['pay_time']);
            }
        }
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }

    /**
     * 添加(更新)支付日志
     * @param $pay_data
     * @param string $pay_type
     * @param string $pay_mode
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function addLog($pay_data, $pay_type = 'pay', $pay_mode = 'weixin')
    {
        $extract = 0;
        if (isset($pay_data['extract'])) {
            $extract = intval($pay_data['extract']);
        }
        $map = [
            'pay_type' => $pay_type,
            'order_sn' => $pay_data['order_sn']
        ];
        $log = self::where($map)->find();
        if (empty($log)) {
            $in_data = [
                'pay_type' => $pay_type,
                'order_sn' => $pay_data['order_sn'],
                'pay_sn' => $pay_data['pay_sn'],
                'pay_mode' => $pay_mode,
                'extract' => $extract,
                'total' => $pay_data['total'],
                'add_date' => date("Y-m-d H:i:s"),
                'add_time' => time()
            ];
            $ret = self::insert($in_data);
        } else {
            $in_data = [
                'pay_type' => $pay_type,
                'order_sn' => $pay_data['order_sn'],
                'pay_sn' => $pay_data['pay_sn'],
                'pay_mode' => $pay_mode,
                'extract' => $extract,
                'total' => $pay_data['total'],
                'add_date' => date("Y-m-d H:i:s"),
                'add_time' => time()
            ];
            $ret = self::where($map)->update($in_data);
        }
        if ($ret !== false) {
            return true;
        }
        return false;
    }

    /**
     * 获取支付日志
     * @param $pay_sn
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getLog($pay_sn)
    {
        $log = self::where(['pay_sn' => $pay_sn])->find();
        if (empty($log)) {
            return [];
        }
        return $log->toArray();
    }

    /**
     * 获取支付日志 金额统计
     * @param $pay_sn
     * @return float|int
     */
    public static function getLogTotal($pay_sn)
    {
        $total = self::where(['pay_sn' => $pay_sn])->sum('total');
        if (empty($total)) {
            return 0;
        }
        return $total;
    }

    /**
     * 跟新支付日志状态
     * @param $log
     * @param $pay_mode
     * @return bool
     */
    public static function updateLog($log, $pay_mode = 'weixin')
    {
        $map = [
            'pay_sn' => $log['pay_sn']
        ];
        $up_data = [
            'pay_mode' => $pay_mode,
            'pay_time' => time(),
            'status' => 1,
        ];
        $ret = self::where($map)->update($up_data);
        if ($ret !== false) {
            return true;
        }
        return false;
    }

    /**
     * 获取支付信息
     * @param $order_sn
     * @param $pay_type
     * @return array|bool|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getPayLog($order_sn, $pay_type = 'pay')
    {
        $map = [
            'pay_type' => $pay_type,
            'order_sn' => $order_sn
        ];
        $pay_log = self::where($map)->find();
        if (!empty($pay_log)) {
            return $pay_log;
        }
        return false;
    }
}
